﻿Ext.define('TAB.view.user.ConditionForm', {
    extend: 'Ext.form.Panel',
    alias: 'widget.UserConditionForm',
    requires: [
        'Ext.ux.TreePicker',
        'BIM.store.Option'
    ],
    defaults: {
        xtype: 'textfield',
        labelWidth: 55,
        labelAlign: 'right',
        anchor: '95%'
    },
    bodyStyle: 'padding:5px 0px 5px 0px',
    itemId: 'conditionForm',
    items: [{
        name: 'FREALNAME',
        fieldLabel: '姓名'
    }, {
        name: 'FNAME',
        fieldLabel: '用户名',
    }, {
        name: 'FJOBNUMBER',
        fieldLabel: '工号'
    }, {
        name: 'FSEX',
        fieldLabel: '性别',
        xtype: 'combo',
        multiSelect: false,
        forceSelection: true,
        queryMode: 'local',
        store: Ext.create('Ext.data.Store', {
            fields: ['value', 'text'],
            data: [{
                value: '男',
                text: '男'
            }, {
                value: '女',
                text: '女'
            }]
        }),
        queryMode: 'local',
        valueField: 'value',
        displayField: 'text'
    }, {
        fieldLabel: '职务',
        COperator: '0',
        xtype: 'combo',
        itemId: 'comboDuty',
        multiSelect: false,
        displayField: 'FNAME',
        valueField: 'FID',
        queryMode: 'local',
        typeAhead: true,
        forceSelection: true,
        listConfig:{
            loadMask: false
        },
        name: 'FDUTYID'
    }, {
        fieldLabel: '部门',
        IgnoreField: true, //xzh不按默认处理方式,为了区分是自定义的属性，这里首字母大写
        IgnoreValue: 'root',
        xtype: 'treepicker',
        itemId: 'comboDept',
        name: 'FDEPARTMENTID',
        displayField: 'text',
        valueField: 'FID',
        listConfig:{
            loadMask: false
        },
        store: Ext.create('Ext.data.TreeStore', {
            fields: ['text', 'level', 'children', 'code'],
            root:{
                text: '组织结构',
                id:''
            }
        })

    }, {
        name: 'FPHONENUMBER',
        xtype: 'numberfield',
        fieldLabel: '手机号'
    }, {
        name: 'FTELNUMBER',
        fieldLabel: '固话'
    }, {
        name: 'FEMAIL',
        fieldLabel: '邮箱',
        vtype: 'email'
    }, {
        xtype: 'fieldcontainer',
        layout: 'hbox',
        defaultType: 'textfield',
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 55
        },
        items: [{
            xtype: 'combo',
            fieldLabel: '生日',
            name: 'birthdayCompare',
            width: 145,
            forceSelection: true,
            queryMode: 'local',
            IgnoreField: true,
            listConfig: {
                loadMask: false
            },
            valueField: 'value',
            displayField: 'text',
            value: 3,
            store: Ext.create('Ext.data.Store', {
                fields: ['text', 'value'],
                data: [{
                    text: '等于',
                    value: 0
                }, {
                    text: '大于',
                    value: 2
                }, {
                    text: '小于',
                    value: 3
                }, {
                    text: '大于等于',
                    value: 4
                }, {
                    text: '小于等于',
                    value: 5
                }]
            })
        }, {
            name: 'FBIRTHDAY',
            xtype: 'datefield',
            format: 'Y-m-d',
            altFormats: 'Y-m-d',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            invalidText: '日期格式不对，格式形如：2013-05-03',
            IgnoreField: true,
            labelWidth: 0,
            margin: '0 0 0 3',
            width: 100
       }]
    }, {
        name: 'FISACTIVE',
        fieldLabel: '状态',
        xtype: 'combo',
        multiSelect: false,
        forceSelection: true,
        store: Ext.create('Ext.data.Store', {
            fields: ['value', 'text'],
            data: [{
                value: '1',
                text: '正常'
            }, {
                value: '0',
                text: '锁定'
            }]
        }),
        queryMode: 'local',
        valueField: 'value',
        displayField: 'text'
    }, {
        xtype: 'textarea',
        name: 'FREMARK',
        fieldLabel: '备注',
        height:40
    }, {
        xtype: 'button',
        text: '清空条件',
        height: 40,
        anchor: '100%',
        margin: '0 10px 5px',
        handler: function () {
            this.up('form').getForm().reset();
            this.up('form').getForm().findField('FDEPARTMENTID').treeCode = '1';
        }
    }, {
        xtype: 'button',
        text: '综合查询',
        height: 40,
        anchor: '100%',
        margin: '0 10px',
        itemId: 'btnQuery',
        handler: function () {
            var values = this.up('form').getQueryValues();
            if (typeof this.up('form').btnQuery == "function") this.up('form').btnQuery(values);
        }
    }],
    initComponent: function () {
        this.callParent(arguments);
        //职务
        var comboDuty = this.down('#comboDuty');
        comboDuty.store = Ext.create('BIM.store.Option', {
            autoLoad: true,
            proxy: {
                type: 'ajax',
                url: Twi.Url,
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount',
                    successProperty: 'success'
                },
                extraParams: {
                    cn: 'PageCommon',
                    mn: 'TAB',
                    mn2: 'GetBySimpleCondition',
                    data: Ext.encode({
                        TableName: 'SM004',
                        OrderFields: 'FSORT',
                        Condition:
                        {
                            FIsDeleted: 0,
                            FOptionTypeID: '846ca4f6-ae5c-4293-be47-332dd5604fc9'
                        }
                    })
                }
            }
        });

        //部门
        var comboDept = this.down('#comboDept');
        Twi.QuickAjax.Ajax({
            loadMask:false,
            params: {
                cn: 'Tree',
                mn: 'TAB',
                mn2: 'GetTreeData',
                value: '1'
            },
            success: function (twi) {
                comboDept.store.setRootNode(twi.data[0]);
            }
        });

        //enter事件
        var me = this;
        Ext.each(this.getForm().getFields().items, function (item, index) {
            item.on('specialkey', function (field, e) {
                if (e.getKey() == e.ENTER) {
                    me.down('#btnQuery').handler();
                }
            });
        });

    } //end initComponent

    , getQueryValues: function () {
        var values = [],
            fields = this.getForm().getFields().items,
            f,
            fLen = fields.length,
            field, val;
        for (f = 0; f < fLen; f++) {
            field = fields[f];
            val = field.getValue();
            //忽略的值
            if (field.IgnoreValue) {
                val = val == field.IgnoreValue ? undefined : val;
            }
            //不按默认处理方式
            if (field.IgnoreField) {
                if (field.getName() == "FDEPARTMENTID") {
                    val = ""; //使之不会放入values数组中
                    //当为根节点的时候，就没必要查询了
                    if (field.treeCode != '1' && field.treeCode) {
                        values.push({
                            Field: 'FDEPARTMENTTCODE',
                            Value: field.treeCode,
                            JOperator: 0,
                            COperator: 7
                        });
                    }
                }
            }
            else if (field.getName() == "FBIRTHDAY") {
                    if (val) {
                        values.push({
                            Field:'FBIRTHDAY',
                            Value: val,
                            JOperator:0,
                            COperator:this.getForm().findField('birthdayCompare').getValue()
                        });
                    }
                    val = "";
                }
            if (val && val.length > 0) {
                values.push({
                    Field: field.getName(),
                    Value: Ext.isArray(val) ? val.join(',') : val, //多选的时候用逗号隔开
                    JOperator: parseInt(field.JOperator == undefined ? 0 : field.JOperator),
                    COperator: parseInt(field.COperator == undefined ? 6 : field.COperator)
                });
            }
        }
        return values;
    } //end getQueryValues
});
